<template>
  <div class="home-container">
    <!-- 快捷入口 -->
    <div class="enter-container">
      <div class="enter-list">
        <div
          class="enter-item"
          v-for="item in state.entryList"
          :key="item.id"
          :style="{ background: item.bg }"
          @click="entryItemClick(item)"
        >
          <div class="enter-icon">
            <van-image
              width="1.6rem"
              height="1.6rem"
              fit="cover"
              :src="item.icon"
            ></van-image>
          </div>
          <div class="enter-text">{{ item.name }}</div>
        </div>
      </div>
    </div>

    <!-- 打卡记录 -->
    <div class="task-info">
      <div class="task-info__header">
        <div class="task-info__header-left">
          <van-image
            round
            width="2.5rem"
            height="2.5rem"
            fit="cover"
            :src="userAvatar"
          />
          <h3>打卡记录</h3>
        </div>
        <div
          class="task-info__header-right"
          @click="
            () => {
              router.push({
                name: 'taskList',
              });
            }
          "
        >
          <span>更多</span>
          <van-icon name="more-o" size="22px" />
        </div>
      </div>
      <div class="taask-info__content">
        <CustomCard
          :item="item"
          v-for="item in state.taskList"
          :key="item.id"
          @click="goDetail(item)"
        />
      </div>
    </div>

    <div class="task-info">
      <div class="task-info__header">
        <div class="task-info__header-left">
          <van-image
            round
            width="2.5rem"
            height="2.5rem"
            fit="cover"
            :src="userAvatar"
          />
          <h3>消费记录</h3>
        </div>
        <div
          class="task-info__header-right"
          @click="
            () => {
              router.replace({
                name: 'shopping',
              });
            }
          "
        >
          <span>更多</span>
          <van-icon name="more-o" size="22px" />
        </div>
      </div>
      <div class="taask-info__content">
        <CustomCard
          type="shopping"
          :item="item"
          v-for="item in state.coinRecordList"
          :key="item.id"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import dayjs from "dayjs";
import { getUserTaskListForPages } from "@/service/task";
import { getCoinRecordForPages } from "@/service/user";
import { useAppStore } from "../../stores";
import { showDialog } from "vant";
import userAvatar from "@/assets/images/avatar.png";
import taskIcon from "@/assets/images/task_white.png";
import bookIcon from "@/assets/images/book.png";
import AIIcon from "@/assets/images/robot.png";

import CustomCard from "@/components/CustomCard/Index.vue";
const router = useRouter();
const appStore = useAppStore();

const currentYear = ref(2023);
const state = reactive({
  yearOptions: [
    { text: "2022", value: 2022 },
    { text: "2023", value: 2023 },
    { text: "2024", value: 2024 },
  ],
  entryList: [
    {
      id: 1,
      name: "我的打卡",
      bg: "#78669c",
      icon: taskIcon,
      routeName: "taskList",
    },
    {
      id: 2,
      name: "图书类别",
      bg: "#8d7faa",
      icon: bookIcon,
      routeName: "libraryModel",
      routerQuery: "1",
    },
    {
      id: 3,
      name: "图书管理",
      bg: "#8d7faa",
      icon: bookIcon,
      routeName: "libraryModel",
      routerQuery: "2",
    },
    {
      id: 4,
      name: "AI助手",
      bg: "#78669c",
      icon: AIIcon,
      routeName: "",
    },
  ], //入口列表

  taskList: [], //打卡记录列表

  coinRecordList: [], //消费列表
});

//   获取打卡记录
const getTaskList = async () => {
  const { data } = await getUserTaskListForPages({
    pageSize: 3,
  });

  state.taskList = data.list;
};

//   获取打卡记录
const getCoinRecordList = async () => {
  const { data } = await getCoinRecordForPages({
    pageSize: 3,
  });

  state.coinRecordList = data.list;
};

//入口跳转
const entryItemClick = (item) => {
  if (!item.routeName) {
    showDialog({
      title: "提示",
      message: "功能正在开发中",
    }).then(() => {
      // on close
    });

    return;
  }
  router.push({
    name: item.routeName,
    query: {
      type: item.routerQuery,
    },
  });
};

appStore.setAppNav("navTitle", "首页");
getTaskList();
getCoinRecordList();

const goDetail = (item) => {
  router.push({
    name: "taskDetail",
    query: {
      id: item.id,
    },
  });
};
</script>

<style scoped lang="less">
@primary-bg: var(--primaryBg, #78669c);
@second-bg: var(--secondBg, #8d7faa);

.home-container {
  overflow-y: scroll;
  background: linear-gradient(
      rgba(255, 255, 255, 0.8),
      /* 这里使用半透明的白色 */ rgba(255, 255, 255, 0.8)
    ),
    url("../../assets/images/home_bg.png"); /* 背景图片路径 */
  background-blend-mode: lighten; /* 使用混合模式 */

  .enter-container {
    .enter-list {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;

      .enter-item:nth-child(2n) {
        background: @second-bg;
      }
      .enter-item {
        padding: 14px 20px;
        border-radius: 4px;
        background: @primary-bg;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex: 1 1 calc(50% - 10px);
        box-sizing: border-box;
        color: #fff;

        .enter-icon {
        }

        .enter-text {
          font-size: 18px;
        }
      }
    }
  }

  .task-info {
    padding: 20px;
    .task-info__header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-bottom: 16px;

      .task-info__header-left {
        display: flex;
        align-items: center;
        gap: 10px;
      }
      .task-info__header-right {
        display: flex;
        align-items: center;
        gap: 10px;
      }
    }
    .taask-info__content {
      .task-card {
        border: 2px solid #78669c;
        border-radius: 8px;
        padding: 12px 16px;
        color: #000;
        font-size: 16px;
        margin-bottom: 10px;
        background: linear-gradient(
            rgba(255, 255, 255, 0.2),
            /* 这里使用半透明的白色 */ rgba(255, 255, 255, 0.2)
          ),
          url("../../assets/images/kuluomi-task.png");
        background-blend-mode: lighten; /* 使用混合模式 */

        background-position: right 90px bottom; /* 定位到右下角 */
        background-repeat: no-repeat; /* 防止背景图片重复 */
        background-size: 55px;

        & > div {
          display: flex;
          justify-content: space-between;
          align-items: center;
          line-height: 2;
        }
      }
    }
  }
}
</style>
